import { Directive, ElementRef, HostListener, Output, EventEmitter, } from '@angular/core';

@Directive({
  selector: '[ccTrClick]'
})

export class ClicekDirective {

  constructor(
    private el: ElementRef
  ) { }

  @Output() singleClick = new EventEmitter<any>();

  @Output() doubleClick = new EventEmitter<any>();

  /**
   * 单双击间隔
   */
  private delay = 200;

  /**
   * 记录单击数量
   */
  private clicks = 0;

  /**
   * 定时器
   */
  private timer = null;

  @HostListener('click')
  click(): void {
    this.clicks++;
    if (this.clicks === 1) {
      // 单击
      this.timer = setTimeout(() => {
        this.clicks = 0;
        this.singleClick.emit(this.el);
      }, this.delay);
    } else {
      // 双击
      clearTimeout(this.timer);
      this.clicks = 0;
      this.doubleClick.emit(this.el);
    }
  }

}
